package com.yehun.dao.impl;

import com.yehun.dao.RouteDao;
import com.yehun.entity.Route;
import com.yehun.mapper.RouteMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class RouteDaoImpl implements RouteDao {

    @Resource
    private RouteMapper routeMapper;

    @Override
    public List<Route> listAll() {
        return routeMapper.listAll();
    }

    @Override
    public Route get(int id) {
        return routeMapper.get(id);
    }

    @Override
    public Long getMaxValue() {
        return routeMapper.getMaxValue();
    }

    @Override
    public Route getByUrl(String url) {
        return routeMapper.getByUrl(url);
    }

    @Override
    public int save(Route route) {
        long tmpValue;
        Long maxValueObject = routeMapper.getMaxValue();
        long maxValue = maxValueObject == null ? 0L : maxValueObject;
        int i = 0;
        while (true) {
            i++;
            tmpValue = (long)Math.pow(2, i);
            if (tmpValue > maxValue) {
                break;
            }
        }
        route.setValue(tmpValue);
        return routeMapper.save(route);
    }

    @Override
    public int update(Route route) {
        return routeMapper.update(route);
    }

    @Override
    public int updateByUrl(Route route) {
        return routeMapper.updateByUrl(route);
    }

    @Override
    public int remove(Integer[] idArray) {
        return routeMapper.remove(idArray);
    }
}
